package com.jgp.security.service;

import com.jgp.form.model.VirtualForm;
import com.jgp.form.service.FormService;
import com.jgp.security.props.SecurityConfigPropBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-06
 */
@Slf4j
@Service
@Transactional(transactionManager = "secondTransactionManager",readOnly = true)
public class SecInfoDetailsService implements CommandLineRunner {
    
    @Autowired
    private FormService formService;
    
    @Autowired
    private SecurityConfigPropBean configPropBean;
    
    private static String USER_DETAILS_FORM_CODE;
    private static String INSTITUTION_DETAILS_FORM_CODE;
    
    
    
    @Override
    public void run(String... args) {
        
        USER_DETAILS_FORM_CODE = configPropBean.getUserExtFormCode();
        INSTITUTION_DETAILS_FORM_CODE = configPropBean.getInstitutionExtFormCode();
        
        if(configPropBean.getEnabledUserExt()&&!formService.exitFormByCode(USER_DETAILS_FORM_CODE)){
            createUserForm(USER_DETAILS_FORM_CODE);
            log.debug("用户信息扩展表，构建完毕！");
        }
    
        if(configPropBean.getEnabledInstitutionExt()&&!formService.exitFormByCode(INSTITUTION_DETAILS_FORM_CODE)){
            createInstitutionForm(INSTITUTION_DETAILS_FORM_CODE);
            log.debug("机构信息扩展表，构建完毕！");
        }
        
    }
    
    @Transactional(transactionManager = "secondTransactionManager")
    public void createUserForm(String code){
    
        VirtualForm form = new VirtualForm();
        form.setCode(code);
        form.setName("用户扩展信息");
        form.setCol(12);
        form.setRemark("用于扩展用户其他属性");
        
        formService.saveForm(form);
    }
    
    @Transactional(transactionManager = "secondTransactionManager")
    public void createInstitutionForm(String code){
        
        VirtualForm form = new VirtualForm();
        form.setCode(code);
        form.setName("组织机构扩展信息");
        form.setCol(12);
        form.setRemark("用于扩展组织机构其他属性");
        
        formService.saveForm(form);
    }
}
